/* 
 * File:   claveString.cpp
 * Author: jimena2
 * 
 * Created on 31 de mayo de 2011, 10:44 PM
 */

#include "claveString.h"

#include <stddef.h>
#include <stdexcept>
#include <string.h>

claveString::claveString(const char* str){
    if(str == NULL)
        throw std::invalid_argument("str es NULL");

    int strSize = strlen(str);
    s = strcpy(new char[strSize+1], str);

    // calcula el hash del string una sola vez utilizando
    //
    precalculatedHash = 0;
    for(const char *is = s; *is != '\0'; ++is)
        precalculatedHash += 33 * precalculatedHash + *is;
}

int claveString::hash()
{
    return  precalculatedHash;
}

bool claveString::equals(IKey *k)
{
    claveString *str = dynamic_cast<claveString *>(k);
    if(str == NULL)
        throw std::invalid_argument("Invalid key k");
    else
        return strcmp(s, str->s) == 0;
}

const char *claveString::getValue()
{
    return s;
}

claveString::~claveString(){
    delete[] s;
}